package org.jeecg.modules.teacher.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.student.entity.DiyStudent;
import org.jeecg.modules.teacher.entity.DiyTescher;
import org.jeecg.modules.teacher.mapper.DiyTescherMapper;
import org.jeecg.modules.teacher.service.IDiyTescherService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description: 教师表
 * @Author: jeecg-boot
 * @Date:   2021-05-08
 * @Version: V1.0
 */
@Service
public class DiyTescherServiceImpl extends ServiceImpl<DiyTescherMapper, DiyTescher> implements IDiyTescherService {

    @Override
    public void saveOrUpdateInfo(JSONArray form_component_values, String originator_userid) {
        DiyTescher teacher = new DiyTescher();
        for (int i = 0; i < form_component_values.size(); i++) {
            JSONObject jsonObject = form_component_values.getJSONObject(i);
            String name = jsonObject.getString("name");
            String value = jsonObject.getString("value");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            switch (name){
                case "教师姓名":
                    teacher.setDiyName(value);
                    break;
                case "教师工号":
                    teacher.setDiyNumber(value);
                    break;
                case "教师系别":
                    teacher.setDiyDepartment(value);
                    break;
                case "教师性别":
                    teacher.setDiySex(value);
                    break;
                case "教师电话":
                    teacher.setDiyPhone(value);
                    break;
            }

            teacher.setDiyDinguserid(originator_userid);
            System.out.println(teacher);

            List<DiyTescher> diyStudents = baseMapper.selectList(new LambdaQueryWrapper<DiyTescher>().eq(DiyTescher::getDiyDinguserid, teacher.getDiyDinguserid()));
            if (diyStudents!=null && diyStudents.size()>0){
                baseMapper.update(teacher,new LambdaQueryWrapper<DiyTescher>().eq(DiyTescher::getDiyDinguserid,teacher.getDiyDinguserid()));
            }else {
                baseMapper.insert(teacher);
            }
        }
    }
}
